import carList from '@/components/waterfall/carList.vue'//数据列表
import recommend from '@/components/waterfall/recommend.vue'//好车推荐列表
export default {
  components: {
    carList,
    recommend
  },
  data() {
    return {
      isLoading: false,
      carlist: [], //好车推荐
      list: [],
      show: true, //历史搜索
      searchKey: '',
      hotList: [],
      history: [],
      loadStatus: 'loading',
      carIndex: 1, //当前页
      listShow: false, //车源列表
      searchList: [], //关键则列表
      recommend: true, //是否显示i推荐
      isSearch: false, //是否显示关键字
      isOpen: false, //开关
      isSj: false, //暂无数据
      touchStartX: 0,
      touchStartY: 0,
      isBlock: true,
      clientY: 0,
    }
  },

  created() {
    this.getCarhot()
    this.getSearchGoods()
    this.getCarHotList()
  },
  onShow() {
    this.isOpen = false
    this.isSearch = false
    this.SearchCarList()
  },
  mounted() {
    //#ifdef H5
    //核心开始------------------------------------
    let oldTop = 0 //旧数据，初始为0
    // 将自定义方法绑定到窗口滚动条事件
    window.onscroll = () => {
      let top = document.scrollingElement.scrollTop //触发滚动条，记录数值
      //旧数据大于当前位置，表示滚动条top向上滚动
      if (oldTop > top) {
        this.isBlock = true
      } else {
        //相反...
        this.isBlock = false
      }
      oldTop = top //更新旧的位置
    }
    //#endif
  },
  onPageScroll(e) {
    //#ifdef APP-PLUS
    /* let oldTop = 0 //旧数据，初始为0
		let top = e.scrollTop //触发滚动条，记录数值
		//旧数据大于当前位置，表示滚动条top向上滚动
		if (oldTop >= top) {
		  this.isBlock = true

		} else {
		  //相反...
		  this.isBlock = false
		}
		oldTop = top //更新旧的位置 */
    //#endif
  },
  onLoad() {
    //历史
    uni.getStorage({
      key: 'searchLocal',
      success: res => {
        if (res.data !== '') {
          this.history = JSON.parse(res.data)
        }
      },
      fail: err => {

      }
    })
  },
  watch: {
    searchKey(val) {
      if (val === '') {
        this.recommend = true
        this.isLoading = false
        this.isSearch = false
        this.show = true
        this.isSj = false
      }
    }
  },
  methods: {
    touchmove(e) {
      //#ifdef APP-PLUS
      let top = e.changedTouches[0].clientY
      if(this.isSearch || this.listShow)return false
      if (top > this.clientY) {
        this.isBlock = true
      } else {
        this.isBlock = false
      }
      //#endif
    },
    touchstart(e) {
      //#ifdef APP-PLUS
      this.clientY = e.changedTouches[0].clientY
      //#endif
    },
    focus() {
      this.isOpen = true
      // this.SearchCarList()
    },
    //搜索change
    change(e) {
      this.searchKey = e
      if (!this.isOpen) return false
      this.listShow = false
      this.isLoading = true
      this.isSearch = true
      this.$u.debounce(this.SearchCarList, 500)
    },
    //实时关键字
    async SearchCarList() {
      if (!this.isOpen) {
        return false
      }
      try {
        let data = {
          'keyWords': this.searchKey,
          'rows': 10
        }
        const res = await this.$api.common.SearchCarList(data)
        this.searchList = res.data
        if (this.searchKey === '') {
          this.searchList = []
          this.isSearch = false
          this.isLoading = false
          this.show = true
          this.recommend = true
          this.listShow = false //列表
          return false
        }
        this.isSj = true
        this.isOpen = true
        this.isLoading = false
        this.show = false
        this.recommend = false
        // this.listShow = true//列表
      } catch (e) {
        //TODO handle the exception
      }
    },

    //点击关键字
    onSearchList(list) {
      this.carIndex = 1
      this.list = []
      this.isOpen = false
      this.isSearch = false
      if (list.type === 0) {
        this.searchKey = list.name
        this.show = false
        this.recommend = false
        this.listShow = true
        this.getSearchGoods()
      } else if (list.type === 1) {
        uni.navigateTo({
          url: '/pages_commodity/sourceDetails/sourceDetails?goodid=' + list.id
        })
      }
    },
    async getList(val) {
      // this.isOpen = false
      uni.showLoading({
        title: '加载中',
        mask: true
      })
      this.loadStatus = 'loading'
      try {
        let data = {
          'pageIndex': this.carIndex,
          'pageSize': 10,
          'search': val
        }
        const res = await this.$api.buyCar.getSearchGoods(data)
        console.log(res, '列表')
        this.list = [...this.list, ...res.data.list]
        uni.hideLoading()
        this.moreStatus(res.data.list.length !== 0 ? 'loadmore' : 'nomore')
        if (res.data.list.length === 0) return false
        this.carIndex += 1 // 当前页加1
      } catch (e) {
        //TODO handle the exception
      }
    },
    //加载更多
    searchMore() {
      this.getSearchGoods()
    },
    //好车推荐
    async getCarHotList() {
      try {
        const res = await this.$api.buyCar.getCarHotList()
        this.carlist = res.data
      } catch (e) {
        //TODO handle the exception
      }
    },
    //热门搜索
    hotSearch(list) {
      this.isOpen = false
      this.list = []
      this.carIndex = 1
      this.searchKey = list.name
      this.isSearch = false
      this.listShow = true
      this.show = false
      this.recommend = false
      let newArr = this.history
      //如果有搜索相同的话，先删除，再添加新的
      if (this.history.indexOf(this.searchKey) === -1) {
        newArr.unshift(this.searchKey)
      }
      //超过多少个就删除最后一个
      if (newArr.length >= 10) {
        newArr.pop()
      }
      newArr = JSON.stringify(newArr) //转换成json格式的数据
      uni.setStorage({ //将搜索的值添加到本地缓存
        key: 'searchLocal',
        data: newArr
      })
      this.getSearchGoods()
    },
    //历史搜索
    onhistory(item) {
      this.isOpen = false
      this.list = []
      this.carIndex = 1
      this.searchKey = item
      this.isSearch = false
      this.listShow = true
      this.show = false
      this.recommend = false
      this.getSearchGoods()
    },
    // 加载前、中、后
    moreStatus(status) {
      this.loadStatus = status
    },
    //点击搜索
    djSearch() {
      this.$u.throttle(this.textSearch, 1000)
    },
    textSearch() {
      this.isOpen = false
      this.list = []
      this.carIndex = 1
      if (this.searchKey === '') {
        this.$u.toast('请输入关键字')
        return false
      }
      let newArr = this.history
      //如果有搜索相同的话，先删除，再添加新的
      if (this.history.indexOf(this.searchKey) === -1) {
        newArr.unshift(this.searchKey)
      }
      //超过多少个就删除最后一个
      if (newArr.length >= 10) {
        newArr.pop()
      }
      newArr = JSON.stringify(newArr) //转换成json格式的数据
      uni.setStorage({ //将搜索的值添加到本地缓存
        key: 'searchLocal',
        data: newArr
      })
      this.isSearch = false
      this.listShow = true
      this.recommend = false
      this.getSearchGoods()

    },

    //清楚缓存
    del() {
      var that = this
      uni.setStorage({
        key: 'searchLocal',
        data: that.history
      })
      this.history = []
    },
    //搜索
    onSearch(val) {
      this.list = []
      this.carIndex = 1
      if (val === '') {
        this.$u.toast('请输入关键字')
        return false
      }
      this.searchKey = val
      let newArr = this.history
      //如果有搜索相同的话，先删除，再添加新的
      if (this.history.indexOf(this.searchKey) === -1) {
        newArr.unshift(this.searchKey)
      }
      //超过多少个就删除最后一个
      if (newArr.length >= 10) {
        newArr.pop()
      }
      newArr = JSON.stringify(newArr) //转换成json格式的数据
      uni.setStorage({ //将搜索的值添加到本地缓存
        key: 'searchLocal',
        data: newArr
      })
      this.isSearch = false
      this.$u.throttle(this.getSearchGoods, 500)
      // this.getSearchGoods()
      this.listShow = true
      this.show = false
      this.recommend = false
    },
    //热门搜索
    async getCarhot() {
      try {

        const res = await this.$api.buyCar.getCarhot()
        // console.log(res, '热门')
        this.hotList = res.data
      } catch (e) {
        //TODO handle the exception
      }
    },
    //搜索列表
    async getSearchGoods() {
      this.loadStatus = 'loading'
      uni.showLoading({
        title: '加载中',
        mask: true
      })
      this.isSearch = false
      try {
        let data = {
          'pageIndex': this.carIndex,
          'pageSize': 10,
          'search': this.searchKey
        }
        const res = await this.$api.buyCar.getSearchGoods(data)

        this.list = [...this.list, ...res.data.list]
        if (res.data.total <= 10) {
          this.loadStatus = 'nomore'
        } else {
          this.moreStatus(res.data.list.length !== 0 ? 'loadmore' : 'nomore')
        }
        // this.moreStatus(res.data.list.length !== 0 ? 'loadmore' : 'nomore')
        uni.hideLoading()
        if (res.data.list.length === 0) return false
        this.carIndex += 1 // 当前页加1
      } catch (e) {
        //TODO handle the exception
      }
    }
  }
}
